//
//  ModifyPasswdViewController.m
//  IBaby
//
//  Created by ZengYuan on 14/11/27.
//  Copyright (c) 2014年 com.zhongwei. All rights reserved.
//

#import "ModifyPasswdViewController.h"
#import "InputCell.h"
#import "Config.h"
#import "UserUnit.h"
#import "AppDelegate.h"
#import "MonitorViewController.h"
#import "HomeViewController.h"
@interface ModifyPasswdViewController ()

@end

@implementation ModifyPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=RGB(235, 235, 235);

    // Do any additional setup after loading the view.
    tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    tableView.contentInset = UIEdgeInsetsMake(20, 0.0f, 0.0f, 0.0f);
    
    dataArray=[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"旧密码",@"新密码", nil],nil];

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.titleLabel.text=@"修改密码";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section==0){
        return 80;
    }else{
        return 0;
    }
}


- (UIView *)tableView:(UITableView *)atableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 80)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:btn];
    float y=(view.frame.size.height - 48)/2;
    [btn setFrame:CGRectMake(28, y, view.frame.size.width-28*2, 48)];

    [btn addTarget:self action:@selector(modifyPasswd) forControlEvents:UIControlEventTouchUpInside];
    
    [self setBtnGreenBackgroundImage:btn];
     [btn setTitle:@"保  存" forState:UIControlStateNormal];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    static NSString *CellActivity = @"CellActivity";
    InputCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellActivity];
    if (!cell) {
        cell=[[InputCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellActivity];
    }
    cell.backgroundColor=[UIColor whiteColor];
    NSArray *array=dataArray[indexPath.section];
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    cell.rowType = [self rowIndex:indexPath];
    
    if (indexPath.row == 0) {
        oldPasswdTextField=cell.textfield;
        [oldPasswdTextField becomeFirstResponder];
    }else{
        newPasswdTextField=cell.textfield;
    }
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(int)rowIndex:(NSIndexPath *)indexPath{
    NSInteger rowsCount = [dataArray[indexPath.section] count];
    
    if (rowsCount>1) {
        if (indexPath.row==0) {
            return 1;
        }else if(indexPath.row == rowsCount-1){
            return 3;
        }
        return 2;
        
    }else{
        return 0;
    }
}

-(void)modifyPasswd{
    
    if ([oldPasswdTextField.text length]==0) {
        MBALERT(@"旧密码不能为空");
        return;
    }
    
    if ([newPasswdTextField.text length]==0) {
        MBALERT(@"新密码不能为空");
        return;
    }
    
    
//    if ([newPasswdTextField.text length]< 6 || [oldPasswdTextField.text length]< 6) {
//        MBALERT(@"密码长度不能少于6位");
//        return;
//    }
    
    NSString *url = [NSString stringWithFormat:MAINURL, @"modify_secret.action"];
    __unsafe_unretained typeof(self) weakSelf = self;
    
    UserUnit *me = [UserUnit getActiveUser];
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects:
            @[me.useruuid,oldPasswdTextField.text,newPasswdTextField.text]
            forKeys:@[@"phoneno",@"old_secret",@"new_secret"]];
    NSString *json  =[dic JSONString];
    
    
    [AWHttpTool post:url body:json success:^(id json) {
        if (json) {
             NSInteger sta = [json[@"status"] integerValue];
            
            if (sta == 0) {
                
                me.userPasswd = newPasswdTextField.text;
                [UserUnit resetLoginUnit:me];
                
                MBALERT(@"修改成功");
            }else{
                NSString *err = json[@"msg"];
                MBALERT(err);
             }
        }
    } failure:^(NSError *error) {
        MBALERT(error.domain);
    }];
}

@end
